package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Authority;
import com.itchu.system.domain.SysRole;
import com.itchu.system.service.AuthorityService;
import com.itchu.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private AuthorityService authorityService;

    /**
     * 查找角色的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<SysRole>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<SysRole> list = sysRoleService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询出所有角色
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity<List<SysRole>> AllSysRole() {
        List<SysRole> list = sysRoleService.AllSysRole();
        return ResponseEntity.ok(list);
    }

    /**
     * 保存角色信息
     * @param sysRole
     * @return
     */
    @PostMapping("/save")
    public ResponseEntity<Void> addSysRole(SysRole sysRole) {
        sysRoleService.addSysRole(sysRole);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @PutMapping("/save")
    public ResponseEntity<Void> updateSysRole(SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseEntity<Void> deletSysRole(
            @RequestParam("id")Long id) {
        sysRoleService.deletSysRole(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据角色id查找角色
     * @param id 角色id
     * @return
     */
    @GetMapping("/findById")
    public ResponseEntity<SysRole> findById(
            @RequestParam(value = "id") Long id
    ) {
        SysRole sysRole = sysRoleService.findById(id);
        return ResponseEntity.ok(sysRole);
    }

    /**
     * 更新角色的权限
     * @param rid  角色id
     * @param ids  权限ids
     * @return
     */
    @PutMapping("/authority/update")
    public ResponseEntity<Void> updateRoleAuthorities(@RequestParam("rid") Long rid, @RequestParam("ids") List<Long> ids) {
        sysRoleService.updateRoleAuthorities(rid,ids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 查询该角色所拥有的权限
     * @param id  角色id
     * @return
     */
    @GetMapping("/of/authority")
    public ResponseEntity<List<Authority>> findAuthorityById(
            @RequestParam("id")Long id) {
        List<Authority> list = authorityService.findAuthorityById(id);
        return ResponseEntity.ok(list);
    }
}
